import { _decorator, Color, Component, Graphics, math, MotionStreak, Node, Sprite, Vec2, Vec3 } from 'cc';
import { PingPongManager } from './PingPongManager';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('PingPongItem')
export class PingPongItem extends Component {

    @property(MotionStreak)
    MotionStreak: MotionStreak = null;

    @property(Graphics)
    Graphics: Graphics = null;



    Radius: number = 10;
    Speed: number = 100;
    dir: Vec2 = new Vec2();

    private _color: Color = Color.WHITE;
    private _isPause: boolean = false;

    init(parent: Node, pos: Vec3, radius: number, dir: Vec2, speed: number) {
        this.node.parent = parent;
        this.node.setPosition(pos);
        this.dir = dir;
        this.Radius = radius;
        this.Speed = speed;

        this.drawCircle();
    }

    protected update(dt: number): void {
        if (!PingPongManager.Instance.IsStart) return;
        if (this._isPause) return;
        const move: Vec2 = new Vec2(this.dir.x * this.Speed * dt, this.dir.y * this.Speed * dt);
        const pos: Vec3 = this.node.getPosition();
        let targetPos: Vec3 = pos.clone().add3f(move.x, move.y, 0);

        if (move.y + pos.y <= PingPongManager.Instance.RightDown.y + this.Radius) {
            this.dir.y *= -1;
            targetPos.y = PingPongManager.Instance.RightDown.y + this.Radius - (move.y + pos.y) % (PingPongManager.Instance.RightDown.y + this.Radius);
        }
        else if (move.y + pos.y >= PingPongManager.Instance.LeftUp.y - this.Radius) {
            this.dir.y *= -1;
            targetPos.y = PingPongManager.Instance.LeftUp.y - this.Radius - (move.y + pos.y) % (PingPongManager.Instance.LeftUp.y - this.Radius);
        }

        if (move.x + pos.x <= PingPongManager.Instance.LeftUp.x + this.Radius || move.x + pos.x >= PingPongManager.Instance.RightDown.x - this.Radius) {
            targetPos.x = math.clamp(move.x + pos.x, PingPongManager.Instance.LeftUp.x + this.Radius, PingPongManager.Instance.RightDown.x - this.Radius);
            this.dir = PingPongManager.Instance.getDir(targetPos);
            PingPongManager.Instance.addItem(targetPos.x);
            // PingPongManager.Instance.createItem(targetPos);
            // console.error(this.dir.x, this.dir.y);

        }
        this.node.setPosition(targetPos);
    }

    drawCircle() {
        this._color = this.getRandomBrightColor();
        this.Graphics.clear();
        this.Graphics.circle(0, 0, this.Radius);
        this.Graphics.fillColor = this._color;
        this.Graphics.fill();

        this.MotionStreak.color = this._color;
        this.MotionStreak.stroke = 2 * this.Radius;
    }


    //获取比较亮的颜色
    getRandomBrightColor(): Color {
        let r = Math.floor(128 + Math.random() * 128);
        let g = Math.floor(128 + Math.random() * 128);
        let b = Math.floor(128 + Math.random() * 128);
        return new Color(r, g, b, 255);
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }

}


